<?php
// $Id: FrontController.php 634 2013-01-13 07:02:16Z tomqin@gmail.com $

/**
 * Part of the framework.
 *
 * @package     Controller
 * @version     1.0
 * @copyright   Copyright (C) 2010-2012 SupeFun.com Inc.
 * @author      TomQin <tomqin@gmail.com>
 * @license     http://www.supefun.com/license/
 */

class FrontController extends Controller
{
    public $cityId;   //城市ID
    public $cityTag;  //城市标记
    public $cityName; //城市名
    public $cityInfo = array();

    public function render($tpl) {
        extract($this->var);
        include $this->tpl($tpl);

        $this->afterAction();
        exit();
    }

    //在中间加了一层
    public function tpl($tpl) {
        $tpl  = str_replace('.', DS, $tpl).'.tpl.php';
        $base = BASE_PATH . 'source' .DS. 'template' .DS. 'front';
        $file = $base .DS. $this->cityTag .DS. $tpl;
        if(is_file($file)) return $file;
        else return $base .DS. 'default' .DS. $tpl;
    }

    static public function instance() {
        $cityTag  = substr($_SERVER['HTTP_HOST'], 0, -13); //strlen('sudingke.com') = 13
        $cityTag  = strtolower($cityTag);
        $cityInfo = Base::getConfig('cities.'.$cityTag);
        if(!$cityTag || !$cityInfo){
            header('Location: http://www.sudingke.com'.$_SERVER['REQUEST_URI']);
            exit();
        }

        list( , $module, $controller, $action) = explode('/', $_SERVER['REDIRECT_URL'], 4);

        $object  = Controller::loadController('front', $module, $controller);
        if(!($object && $object instanceof FrontController)){
            $object = new FrontController;
        }
        $object->cityId   = $cityInfo['id'];
        $object->cityTag  = $cityTag;
        $object->cityName = $cityInfo['name'];

        $object->setAction($action);
        $object->run();

        p($object);
    }

}
